package cn.tedu.egmvc.controller;

import cn.tedu.egmvc.mapper.AddressMapper;
import cn.tedu.egmvc.pojo.dto.AddressDTO;
import cn.tedu.egmvc.pojo.dto.UpdateAddressDTO;
import cn.tedu.egmvc.pojo.entity.Address;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;
    @RequestMapping("/address/insert")
    public String insert(AddressDTO addressDTO){
        System.out.println(addressDTO);
        Address address=new Address();
        BeanUtils.copyProperties(addressDTO,address);
        System.out.println(address);
        int num =addressMapper.insert(address);
        return num>0?"添加成功":"添加失败";
    }

    @RequestMapping("/address/select")
    public List<Address> setAll(){
        return addressMapper.selectAll();
    }

    @RequestMapping("/address/delete")
    public String delete(int id){
        int num=addressMapper.delete(id);
        return num>0?"删除成功":"删除失败";
    }

    @RequestMapping("/address/update")
    public String  update(UpdateAddressDTO updateAddressDTO){
        System.out.println(updateAddressDTO);
        Address address=new Address();
        BeanUtils.copyProperties(updateAddressDTO,address);
        int num=addressMapper.update(address);
        return num>0?"修改成功":"修改失败";


    }
}
